#include "pch.h"
#include "IO.h"
#include "ConfigFileManager.h"
#include "SystemManager.h"
#include "SystemLog.h"

extern CSystemLog        gSystemLog;
extern CSystemManager    gSystemMgr;
extern CConfigFileManager gConfigMgr;

CIo::CIo()
{
}

CIo::~CIo()
{
}

void CIo::ShotDown()
{
	m_nExitFlagIo = 1;

	auto sz = m_vIoThread.size();
	if (sz > 0)
	{
		::WaitForMultipleObjects((DWORD)sz, &m_vIoThread[0], TRUE, INFINITE);
	}
	m_vIoThread.clear();
}

int CIo::GetAllInputIo()
{	
	Ixud_ReadDI(gSystemMgr.PEXP32C32_BoardNo, (WORD)0, &gSystemMgr.m_wDIVal[0]);
	Ixud_ReadDI(gSystemMgr.PEXP32C32_BoardNo, (WORD)1, &gSystemMgr.m_wDIVal[1]);
	Ixud_ReadDI(gSystemMgr.PEXP32C32_BoardNo, (WORD)2, &gSystemMgr.m_wDIVal[2]);
	Ixud_ReadDI(gSystemMgr.PEXP32C32_BoardNo, (WORD)3, &gSystemMgr.m_wDIVal[3]);
	return 0;
}

BYTE PISODIO_InputByte(WORD wBoardNo, WORD wPortNo)
{
	DWORD dwDIVal;

	Ixud_ReadDI(wBoardNo, wPortNo, &dwDIVal);
	return((BYTE)(dwDIVal & 0xFF));
}

int CIo::SetAllOutputOff()
{
	//int nErr = 0;
	//for (int i = 0; i < gSystemMgr.m_nIODevNum; i++)
	//{
	//	gSystemMgr.m_wDOVal[i] = 0x00;
	//	nErr = mn_set_do_word(MOTOR_CARD_LINE1, i, 0, gSystemMgr.m_wDOVal[i]);
	//	if (nErr != 0)
	//	{
	//		CString cstrLog;
	//		cstrLog.AppendFormat(L"IO init Outnput err.%d--%d--%d", nErr, i, gSystemMgr.m_nIODevNum);
	//		return nErr;
	//	}
	//}
	return 0;
}

void CIo::StartGetIoInputThread()
{
	ProcIoInput.MemProc = &CIo::GetIoInputThread;
	UINT nThreadId;
	HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ProcIoInput.ThreadProc, (LPVOID)this, 0, &nThreadId);
	if (hThread == NULL)
	{
		return;
	}
	m_vIoThread.push_back(hThread);
}

unsigned int __stdcall CIo::GetIoInputThread()
{
	while (0 == m_nExitFlagIo)
	{
		Sleep(10);
		GetAllInputIo();
	
		gSystemMgr.S00 = (((gSystemMgr.m_wDIVal[0] & 0x00000001) == 0x00) ? ON : OFF);
		gSystemMgr.S01 = (((gSystemMgr.m_wDIVal[0] & 0x00000002) == 0x00) ? ON : OFF);
		gSystemMgr.S02 = (((gSystemMgr.m_wDIVal[0] & 0x00000004) == 0x00) ? ON : OFF);
		gSystemMgr.S03 = (((gSystemMgr.m_wDIVal[0] & 0x00000008) == 0x00) ? ON : OFF);
		gSystemMgr.S04 = (((gSystemMgr.m_wDIVal[0] & 0x00000010) == 0x00) ? ON : OFF);
		gSystemMgr.S05 = (((gSystemMgr.m_wDIVal[0] & 0x00000020) == 0x00) ? ON : OFF);
		gSystemMgr.S06 = (((gSystemMgr.m_wDIVal[0] & 0x00000040) == 0x00) ? ON : OFF);
		gSystemMgr.S07 = (((gSystemMgr.m_wDIVal[0] & 0x00000080) == 0x00) ? ON : OFF);

		gSystemMgr.S08 = (((gSystemMgr.m_wDIVal[1] & 0x00000001) == 0x00) ? ON : OFF);
		gSystemMgr.S09 = (((gSystemMgr.m_wDIVal[1] & 0x00000002) == 0x00) ? ON : OFF);
		gSystemMgr.S10 = (((gSystemMgr.m_wDIVal[1] & 0x00000004) == 0x00) ? ON : OFF);
		gSystemMgr.S11 = (((gSystemMgr.m_wDIVal[1] & 0x00000008) == 0x00) ? ON : OFF);
		gSystemMgr.S12 = (((gSystemMgr.m_wDIVal[1] & 0x00000010) == 0x00) ? ON : OFF);
		gSystemMgr.S13 = (((gSystemMgr.m_wDIVal[1] & 0x00000020) == 0x00) ? ON : OFF);
		gSystemMgr.S14 = (((gSystemMgr.m_wDIVal[1] & 0x00000040) == 0x00) ? ON : OFF);

		if (1 == gSystemMgr.m_bisOpenFlag) 
		{
			gSystemMgr.S15 = (((gSystemMgr.m_wDIVal[1] & 0x00000080) == 0x00) ? OFF : ON);
		}
		else 
		{
			gSystemMgr.S15 = (((gSystemMgr.m_wDIVal[1] & 0x00000080) == 0x00) ? ON : OFF);
		}
		//gSystemMgr.S15 = (((gSystemMgr.m_wDIVal[1] & 0x00000080) == 0x00) ? ON : OFF);
		//gSystemMgr.S15 = (((gSystemMgr.m_wDIVal[1] & 0x00000080) == 0x00) ? OFF : ON);

		gSystemMgr.S16 = (((gSystemMgr.m_wDIVal[2] & 0x00000001) == 0x00) ? ON : OFF);
		gSystemMgr.S17 = (((gSystemMgr.m_wDIVal[2] & 0x00000002) == 0x00) ? ON : OFF);
		gSystemMgr.S18 = (((gSystemMgr.m_wDIVal[2] & 0x00000004) == 0x00) ? ON : OFF);
		gSystemMgr.S19 = (((gSystemMgr.m_wDIVal[2] & 0x00000008) == 0x00) ? ON : OFF);
		gSystemMgr.S20 = (((gSystemMgr.m_wDIVal[2] & 0x00000010) == 0x00) ? ON : OFF);
		gSystemMgr.S21 = (((gSystemMgr.m_wDIVal[2] & 0x00000020) == 0x00) ? ON : OFF);
		gSystemMgr.S22 = (((gSystemMgr.m_wDIVal[2] & 0x00000040) == 0x00) ? ON : OFF);
		gSystemMgr.S23 = (((gSystemMgr.m_wDIVal[2] & 0x00000080) == 0x00) ? ON : OFF);
		gSystemMgr.S24 = (((gSystemMgr.m_wDIVal[3] & 0x00000001) == 0x00) ? ON : OFF);
		gSystemMgr.S25 = (((gSystemMgr.m_wDIVal[3] & 0x00000002) == 0x00) ? ON : OFF);
		gSystemMgr.S26 = (((gSystemMgr.m_wDIVal[3] & 0x00000004) == 0x00) ? ON : OFF);
		gSystemMgr.S27 = (((gSystemMgr.m_wDIVal[3] & 0x00000008) == 0x00) ? ON : OFF);
		gSystemMgr.S28 = (((gSystemMgr.m_wDIVal[3] & 0x00000010) == 0x00) ? ON : OFF);
		gSystemMgr.S29 = (((gSystemMgr.m_wDIVal[3] & 0x00000020) == 0x00) ? ON : OFF);
		gSystemMgr.S30 = (((gSystemMgr.m_wDIVal[3] & 0x00000040) == 0x00) ? ON : OFF);
		gSystemMgr.S31 = (((gSystemMgr.m_wDIVal[3] & 0x00000080) == 0x00) ? ON : OFF);


	}
	return 0;
}


void CIo::SetOutputIo(int nPort, long nValue)
{
	int nCard = nPort / 8;
	int nPoint = nPort % 8;				
	switch (nPoint)
	{
	case 0:
		if(nValue == ON)
			gSystemMgr.m_wDOVal[nCard] |= 0x01;
		if(nValue == OFF)
			gSystemMgr.m_wDOVal[nCard] &= 0xfe;
		break;
	case 1:
		if (nValue == ON)
			gSystemMgr.m_wDOVal[nCard] |= 0x02;
		if (nValue == OFF)
			gSystemMgr.m_wDOVal[nCard] &= 0xfd;
		break;
	case 2:
		if (nValue == ON)
			gSystemMgr.m_wDOVal[nCard] |= 0x04;
		if (nValue == OFF)
			gSystemMgr.m_wDOVal[nCard] &= 0xfb;
		break;
	case 3:
		if (nValue == ON)
			gSystemMgr.m_wDOVal[nCard] |= 0x08;
		if (nValue == OFF)
			gSystemMgr.m_wDOVal[nCard] &= 0xf7;
		break;
	case 4:
		if (nValue == ON)
			gSystemMgr.m_wDOVal[nCard] |= 0x10;
		if (nValue == OFF)
			gSystemMgr.m_wDOVal[nCard] &= 0xef;
		break;
	case 5:
		if (nValue == ON)
			gSystemMgr.m_wDOVal[nCard] |= 0x20;
		if (nValue == OFF)
			gSystemMgr.m_wDOVal[nCard] &= 0xdf;
		break;
	case 6:
		if (nValue == ON)
			gSystemMgr.m_wDOVal[nCard] |= 0x40;
		if (nValue == OFF)
			gSystemMgr.m_wDOVal[nCard] &= 0xbf;
		break;
	case 7:
		if (nValue == ON)
			gSystemMgr.m_wDOVal[nCard] |= 0x80;
		if (nValue == OFF)
			gSystemMgr.m_wDOVal[nCard] &= 0x7f;
		break;
	default:
		break;
	}
	Ixud_WriteDO(gSystemMgr.PEXP32C32_BoardNo, (WORD)nCard, gSystemMgr.m_wDOVal[nCard]);	
	GetOutputIoToY();
}	
	
void CIo::GetOutputIoToY()
{
	gSystemMgr.Y00 = (((gSystemMgr.m_wDOVal[0] & 0x00000001) == 0x00000001) ?  ON : OFF);
	gSystemMgr.Y01 = (((gSystemMgr.m_wDOVal[0] & 0x00000002) == 0x00000002) ?  ON : OFF);
	gSystemMgr.Y02 = (((gSystemMgr.m_wDOVal[0] & 0x00000004) == 0x00000004) ?  ON : OFF);
	gSystemMgr.Y03 = (((gSystemMgr.m_wDOVal[0] & 0x00000008) == 0x00000008) ?  ON : OFF);
	gSystemMgr.Y04 = (((gSystemMgr.m_wDOVal[0] & 0x00000010) == 0x00000010) ?  ON : OFF);
	gSystemMgr.Y05 = (((gSystemMgr.m_wDOVal[0] & 0x00000020) == 0x00000020) ?  ON : OFF);
	gSystemMgr.Y06 = (((gSystemMgr.m_wDOVal[0] & 0x00000040) == 0x00000040) ?  ON : OFF);
	gSystemMgr.Y07 = (((gSystemMgr.m_wDOVal[0] & 0x00000080) == 0x00000080) ?  ON : OFF);

	gSystemMgr.Y08 = (((gSystemMgr.m_wDOVal[1] & 0x00000001) == 0x00000001) ?  ON : OFF);
	gSystemMgr.Y09 = (((gSystemMgr.m_wDOVal[1] & 0x00000002) == 0x00000002) ?  ON : OFF);
	gSystemMgr.Y10 = (((gSystemMgr.m_wDOVal[1] & 0x00000004) == 0x00000004) ?  ON : OFF);
	gSystemMgr.Y11 = (((gSystemMgr.m_wDOVal[1] & 0x00000008) == 0x00000008) ?  ON : OFF);
	gSystemMgr.Y12 = (((gSystemMgr.m_wDOVal[1] & 0x00000010) == 0x00000010) ?  ON : OFF);
	gSystemMgr.Y13 = (((gSystemMgr.m_wDOVal[1] & 0x00000020) == 0x00000020) ?  ON : OFF);
	gSystemMgr.Y14 = (((gSystemMgr.m_wDOVal[1] & 0x00000040) == 0x00000040) ?  ON : OFF);
	gSystemMgr.Y15 = (((gSystemMgr.m_wDOVal[1] & 0x00000080) == 0x00000080) ?  ON : OFF);
	
	gSystemMgr.Y16 = (((gSystemMgr.m_wDOVal[2] & 0x00000001) == 0x00000001) ?  ON : OFF);
	gSystemMgr.Y17 = (((gSystemMgr.m_wDOVal[2] & 0x00000002) == 0x00000002) ?  ON : OFF);
	gSystemMgr.Y18 = (((gSystemMgr.m_wDOVal[2] & 0x00000004) == 0x00000004) ?  ON : OFF);
	gSystemMgr.Y19 = (((gSystemMgr.m_wDOVal[2] & 0x00000008) == 0x00000008) ?  ON : OFF);
	gSystemMgr.Y20 = (((gSystemMgr.m_wDOVal[2] & 0x00000010) == 0x00000010) ?  ON : OFF);
	gSystemMgr.Y21 = (((gSystemMgr.m_wDOVal[2] & 0x00000020) == 0x00000020) ?  ON : OFF);
	gSystemMgr.Y22 = (((gSystemMgr.m_wDOVal[2] & 0x00000040) == 0x00000040) ?  ON : OFF);
	gSystemMgr.Y23 = (((gSystemMgr.m_wDOVal[2] & 0x00000080) == 0x00000080) ?  ON : OFF);

	gSystemMgr.Y24 = (((gSystemMgr.m_wDOVal[3] & 0x00000001) == 0x00000001) ?  ON : OFF);
	gSystemMgr.Y25 = (((gSystemMgr.m_wDOVal[3] & 0x00000002) == 0x00000002) ?  ON : OFF);
	gSystemMgr.Y26 = (((gSystemMgr.m_wDOVal[3] & 0x00000004) == 0x00000004) ?  ON : OFF);
	gSystemMgr.Y27 = (((gSystemMgr.m_wDOVal[3] & 0x00000008) == 0x00000008) ?  ON : OFF);
	gSystemMgr.Y28 = (((gSystemMgr.m_wDOVal[3] & 0x00000010) == 0x00000010) ?  ON : OFF);
	gSystemMgr.Y29 = (((gSystemMgr.m_wDOVal[3] & 0x00000020) == 0x00000020) ?  ON : OFF);
	gSystemMgr.Y30 = (((gSystemMgr.m_wDOVal[3] & 0x00000040) == 0x00000040) ?  ON : OFF);
	gSystemMgr.Y31 = (((gSystemMgr.m_wDOVal[3] & 0x00000080) == 0x00000080) ?  ON : OFF);

	return;
}

